#! /bin/bash
APP_PORT=${1:-3000} 
IMAGE_NAME=chat-app-client

echo "请为容器起一个名字"
read NAME

echo "删除之前的镜像跟容器"
docker image rm -f $IMAGE_NAME:latest || true # 忽略未找到镜像的错误
docker container rm -f $NAME || true #忽略未找到容器的错误

echo "开始构建 docker 镜像"
docker build -t $IMAGE_NAME . || {
	echo "构建镜像失败！"
	exit 1
}

echo "开始运行镜像"
docker run -d -p $APP_PORT:$APP_PORT --name $NAME $IMAGE_NAME || {
	echo "运行容器失败！"
	exit 1
}
	
echo "服务已启动在 http://localhost:$APP_PORT"
